<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>L2LB: socket.h Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
<link href="tabs.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Gerado por Doxygen 1.5.6 -->
<div class="navigation" id="top">
  <div class="tabs">
    <ul>
      <li><a href="index.html"><span>Página&nbsp;principal</span></a></li>
      <li><a href="annotated.html"><span>Estruturas&nbsp;de&nbsp;dados</span></a></li>
      <li class="current"><a href="files.html"><span>Ficheiros</span></a></li>
    </ul>
  </div>
<h1>socket.h</h1><a href="socket_8h.html">Ir para a documentação deste ficheiro.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 
<a name="l00006"></a>00006 <span class="preprocessor">#ifndef SOCKET_H_</span>
<a name="l00007"></a>00007 <span class="preprocessor"></span><span class="preprocessor">#define SOCKET_H_</span>
<a name="l00008"></a>00008 <span class="preprocessor"></span>
<a name="l00009"></a><a class="code" href="socket_8h.html#a260f09b00594e59211ccbbac88213bf">00009</a> <span class="preprocessor">#define INADDR_SEND INADDR_LOOPBACK</span>
<a name="l00010"></a>00010 <span class="preprocessor"></span>
<a name="l00014"></a><a class="code" href="structksock__thread__t.html">00014</a> <span class="keyword">struct </span><a class="code" href="structksock__thread__t.html" title="Thread usada para estabelecer conexões socket.">ksock_thread_t</a>
<a name="l00015"></a>00015 {
<a name="l00019"></a><a class="code" href="structksock__thread__t.html#34e131d5c7d4d36b7213114ebdf869c0">00019</a>     <span class="keyword">struct </span>task_struct *<a class="code" href="structksock__thread__t.html#34e131d5c7d4d36b7213114ebdf869c0" title="Thread onde o socket está executando.">thread</a>;
<a name="l00020"></a>00020     
<a name="l00021"></a>00021     void (*<a class="code" href="structksock__thread__t.html#2fd9f49a71f307cb4f2c1ca3940af8cb">fn</a>)(void);
<a name="l00022"></a>00022     
<a name="l00026"></a><a class="code" href="structksock__thread__t.html#f2c4b6bbf29b36dd0bbf5cf97205330a">00026</a>     <span class="keywordtype">int</span> <a class="code" href="structksock__thread__t.html#f2c4b6bbf29b36dd0bbf5cf97205330a" title="Sinaliza se a thread está em execução (flag).">running</a>;
<a name="l00027"></a>00027     
<a name="l00031"></a><a class="code" href="structksock__thread__t.html#3066dd8c62571dc0017f735319bb815d">00031</a>     <span class="keyword">struct </span>socket *<a class="code" href="structksock__thread__t.html#3066dd8c62571dc0017f735319bb815d" title="Socket para receber mensagens.">sock</a>;
<a name="l00032"></a><a class="code" href="structksock__thread__t.html#b05c8fa2261a2415f74822c88667289b">00032</a>     <span class="keyword">struct </span>sockaddr_in <a class="code" href="structksock__thread__t.html#b05c8fa2261a2415f74822c88667289b">addr</a>;
<a name="l00033"></a>00033     
<a name="l00037"></a><a class="code" href="structksock__thread__t.html#fde47daf4e0183ec4c067c28b210eb4a">00037</a>     <span class="keyword">struct </span>socket *<a class="code" href="structksock__thread__t.html#fde47daf4e0183ec4c067c28b210eb4a" title="Socket para enviar mensagens.">sock_send</a>;
<a name="l00038"></a><a class="code" href="structksock__thread__t.html#1bada36c1d4378aa03ec5eb35b6e8d6b">00038</a>     <span class="keyword">struct </span>sockaddr_in <a class="code" href="structksock__thread__t.html#1bada36c1d4378aa03ec5eb35b6e8d6b">addr_send</a>;
<a name="l00039"></a>00039 };
<a name="l00040"></a>00040 
<a name="l00049"></a><a class="code" href="socket_8h.html#c3204901b688e3cb793a9b01c5d19c9a">00049</a> <span class="keywordtype">int</span> <a class="code" href="socket_8h.html#c3204901b688e3cb793a9b01c5d19c9a" title="Envia mensagem no socket.">ksocket_send</a>(<span class="keyword">struct</span> socket *sock, <span class="keyword">struct</span> sockaddr_in *addr, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *buf, <span class="keywordtype">int</span> len)
<a name="l00050"></a>00050 {
<a name="l00051"></a>00051         <span class="keyword">struct </span>msghdr msg;
<a name="l00052"></a>00052         <span class="keyword">struct </span>iovec iov;
<a name="l00053"></a>00053         mm_segment_t oldfs;
<a name="l00054"></a>00054         <span class="keywordtype">int</span> size = 0;
<a name="l00055"></a>00055         
<a name="l00056"></a>00056         <span class="keywordflow">if</span> (sock-&gt;sk==NULL)
<a name="l00057"></a>00057            <span class="keywordflow">return</span> 0;
<a name="l00058"></a>00058         
<a name="l00059"></a>00059         iov.iov_base = buf;
<a name="l00060"></a>00060         iov.iov_len = len;
<a name="l00061"></a>00061         
<a name="l00062"></a>00062         msg.msg_flags = 0;
<a name="l00063"></a>00063         msg.msg_name = addr;
<a name="l00064"></a>00064         msg.msg_namelen  = <span class="keyword">sizeof</span>(<span class="keyword">struct </span>sockaddr_in);
<a name="l00065"></a>00065         msg.msg_control = NULL;
<a name="l00066"></a>00066         msg.msg_controllen = 0;
<a name="l00067"></a>00067         msg.msg_iov = &amp;iov;
<a name="l00068"></a>00068         msg.msg_iovlen = 1;
<a name="l00069"></a>00069         msg.msg_control = NULL;
<a name="l00070"></a>00070         
<a name="l00071"></a>00071         oldfs = get_fs();
<a name="l00072"></a>00072         set_fs(KERNEL_DS);
<a name="l00073"></a>00073         size = sock_sendmsg(sock,&amp;msg,len);
<a name="l00074"></a>00074         set_fs(oldfs);
<a name="l00075"></a>00075         
<a name="l00076"></a>00076         <span class="keywordflow">return</span> size;
<a name="l00077"></a>00077 }
<a name="l00078"></a>00078 
<a name="l00087"></a><a class="code" href="socket_8h.html#754ef7ec12b37fc69e125121011fcec8">00087</a> <span class="keywordtype">int</span> <a class="code" href="socket_8h.html#754ef7ec12b37fc69e125121011fcec8" title="Recebe mensagem no socket.">ksocket_receive</a>(<span class="keyword">struct</span> socket* sock, <span class="keyword">struct</span> sockaddr_in* addr, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>* buf, <span class="keywordtype">int</span> len)
<a name="l00088"></a>00088 {
<a name="l00089"></a>00089         <span class="keyword">struct </span>msghdr msg;
<a name="l00090"></a>00090         <span class="keyword">struct </span>iovec iov;
<a name="l00091"></a>00091         mm_segment_t oldfs;
<a name="l00092"></a>00092         <span class="keywordtype">int</span> size = 0;
<a name="l00093"></a>00093         
<a name="l00094"></a>00094         <span class="keywordflow">if</span> (sock-&gt;sk==NULL) <span class="keywordflow">return</span> 0;
<a name="l00095"></a>00095         
<a name="l00096"></a>00096         iov.iov_base = buf;
<a name="l00097"></a>00097         iov.iov_len = len;
<a name="l00098"></a>00098         
<a name="l00099"></a>00099         msg.msg_flags = 0;
<a name="l00100"></a>00100         msg.msg_name = addr;
<a name="l00101"></a>00101         msg.msg_namelen  = <span class="keyword">sizeof</span>(<span class="keyword">struct </span>sockaddr_in);
<a name="l00102"></a>00102         msg.msg_control = NULL;
<a name="l00103"></a>00103         msg.msg_controllen = 0;
<a name="l00104"></a>00104         msg.msg_iov = &amp;iov;
<a name="l00105"></a>00105         msg.msg_iovlen = 1;
<a name="l00106"></a>00106         msg.msg_control = NULL;
<a name="l00107"></a>00107         
<a name="l00108"></a>00108         oldfs = get_fs();
<a name="l00109"></a>00109         set_fs(KERNEL_DS);
<a name="l00110"></a>00110         size = sock_recvmsg(sock,&amp;msg,len,msg.msg_flags);
<a name="l00111"></a>00111         set_fs(oldfs);
<a name="l00112"></a>00112         
<a name="l00113"></a>00113         <span class="keywordflow">return</span> size;
<a name="l00114"></a>00114 }
<a name="l00115"></a>00115 
<a name="l00116"></a>00116 <span class="preprocessor">#endif </span><span class="comment">/*SOCKET_H_*/</span>
</pre></div></div>
<hr size="1"><address style="text-align: right;"><small>Gerado em Wed Sep 10 23:05:55 2008 para L2LB por&nbsp;
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.6 </small></address>
</body>
</html>
